package com.zz.yt.lib.bluetooth.base;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.util.storage.LattePreference;
import com.zz.yt.lib.bluetooth.R;

import java.io.IOException;
import java.io.InputStream;

/**
 * 重量类持续读取
 *
 * @author qf
 * @version 1.0
 **/
public abstract class BaseBluetoothRxDelegate extends BaseBluetoothDelegate {


    /**
     * 显示数量
     */
    protected TextView mTextWeight = null;

    /**
     * 显示数量单位
     */
    protected TextView mTextUnit = null;

    /**
     * 输入流读取数据
     */
    protected InputStream is;

    protected boolean bThread = false;


    @Override
    public Object setLayout() {
        return R.layout.blu_delegate_added_weight;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        super.onBindView(savedInstanceState, rootView);

        mTextWeight = rootView.findViewById(R.id.id_text_weight);
        mTextUnit = rootView.findViewById(R.id.id_text_unit);

        rootView.findViewById(R.id.layout_refresh).setOnClickListener(v -> inMac());
        rootView.findViewById(R.id.layout_bt_un).setOnClickListener(v -> onReplace());
        rootView.findViewById(R.id.id_text_replace).setOnClickListener(v -> onReplace());
        rootView.findViewById(R.id.id_btn_submit).setOnClickListener(v -> inSubmitNet());
    }


    /**
     * 是否连接
     */
    protected void inMac() {
        super.inMac();
        RelativeLayout layoutBtOn = mRootView.findViewById(R.id.layout_bt_on);
        LinearLayout layoutBtUn = mRootView.findViewById(R.id.layout_bt_un);
        /*
         * 显示连接设备
         */
        TextView mTextBtEquipment = mRootView.findViewById(R.id.id_text_bt_equipment);
        if (TextUtils.isEmpty(mac)) {
            //一次都没连接过
            layoutBtOn.setVisibility(View.GONE);
            layoutBtUn.setVisibility(View.VISIBLE);
        } else {
            LatteLogger.i(mac);
            //
            layoutBtOn.setVisibility(View.VISIBLE);
            layoutBtUn.setVisibility(View.GONE);
            //显示连接的蓝牙名称
            final String device = LattePreference.getCustomAppProfile(Bluetooth.BLUETOOTH);
            mTextBtEquipment.setText(device);
        }
    }

    @Override
    protected void inRx() {
        //打开接收线程
        try {
            //得到蓝牙数据输入流
            is = mSocket.getInputStream();
        } catch (IOException e) {
            ToastUtils.showShort("接收数据失败！");
            return;
        }
        if (!bThread) {
            inReadThread();
            bThread = true;
        }
    }

    /**
     * 读取数据
     */
    protected abstract void inReadThread();

    /*******************提交数据********************/

    void inSubmitNet() {
        final String weight = mTextWeight.getText().toString().trim();
        double mWeight = Double.parseDouble(weight);
        if (mWeight > 0) {
            onResult(mWeight);
        } else {
            ToastUtils.showShort("获取数据为 0.0" + mTextUnit.getText());
        }
    }


    /**
     * 提交数据
     */
    protected abstract void onResult(double mWeight);

}
